package com.lsk.serviceprice.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lsk.internalcommon.DTO.PriceRule;
import com.lsk.internalcommon.DTO.ResponseResult;
import com.lsk.internalcommon.constant.CommonStatusEnum;
import com.lsk.serviceprice.mapper.PriceRuleMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class PriceRuleService {

    @Autowired
    private PriceRuleMapper priceRuleMapper;

    public ResponseResult add(PriceRule priceRule){
        String cityCode = priceRule.getCityCode();
        String vehicleType = priceRule.getVehicleType();
        priceRule.setFareType(cityCode+"$"+vehicleType);
        //查询数据
        QueryWrapper<PriceRule> wrapper = new QueryWrapper<>();
        wrapper.eq("city_code",cityCode);
        wrapper.eq("vehicle_type",vehicleType);
        wrapper.orderByDesc("fare_version");

        List<PriceRule> list = priceRuleMapper.selectList(wrapper);
        int version = 0;
        if (list.size()>0){
            return ResponseResult.fail(CommonStatusEnum.PRICE_RULE_EXIT.getCode(),CommonStatusEnum.PRICE_RULE_EXIT.getValue());
        }
        priceRule.setFareVersion(++version);
        //插入数据
        priceRuleMapper.insert(priceRule);
        return ResponseResult.success();
    }

    /**
     * 修改计价规则
     * @param priceRule
     * @return
     */
    public ResponseResult edit(PriceRule priceRule){
        //拼接fareType
        String cityCode = priceRule.getCityCode();
        String vehicleType = priceRule.getVehicleType();
        String fareType = cityCode + "$" +vehicleType;
        priceRule.setFareType(fareType);

        QueryWrapper<PriceRule> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("city_code",cityCode);
        queryWrapper.eq("vehicle_type",vehicleType);
        queryWrapper.orderByDesc("fare_version");
        List<PriceRule> list = priceRuleMapper.selectList(queryWrapper);

        int version = 0;
        //判断计价规则是否一致
        if (list.size()>0){
            PriceRule rule = list.get(0);
            Double unitPricePerMile = rule.getUnitPricePerMile();
            Double unitPricePerMinute = rule.getUnitPricePerMinute();
            Double startFare = rule.getStartFare();
            Integer startMile = rule.getStartMile();

            if (unitPricePerMile.doubleValue()==priceRule.getUnitPricePerMile().doubleValue()
                && unitPricePerMinute.doubleValue()==priceRule.getUnitPricePerMinute().doubleValue()
                && startFare.doubleValue() == priceRule.getStartFare().doubleValue()
                && startMile.doubleValue() == priceRule.getStartMile()){
                return ResponseResult.fail(CommonStatusEnum.PRICE_RULE_NOT_EDIT.getCode(),CommonStatusEnum.PRICE_RULE_NOT_EDIT.getValue());
            }

            version = rule.getFareVersion();
        }
        priceRule.setFareVersion(++version);
        priceRuleMapper.insert(priceRule);

        return ResponseResult.success("修改成功");
    }
}
